package com.ljx.blog.oss.service.impl;

import com.ljx.blog.common.exception.ApiException;
import com.ljx.blog.oss.domain.vo.req.UploadReq;
import com.ljx.blog.oss.domain.vo.resp.UploadResp;
import com.ljx.blog.oss.service.OssService;
import com.ljx.blog.oss.service.OssStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-24  22:15
 */
@Service
public class OssServiceImpl implements OssService {

    @Value("${oss.type}")
    private Integer type;

    @Override
    public UploadResp upload(UploadReq req) {
        OssStrategy ossStrategy = getOssStrategy();
        if (Objects.isNull(ossStrategy)){
            throw new ApiException("无有效的OSS");
        }
        if (Objects.isNull(req.getFile()) || req.getFile().getSize() == 0){
            throw new ApiException("文件不能为空");
        }
        return ossStrategy.upload(req);
    }

    private OssStrategy getOssStrategy() {
        switch (type){
            case 1:
                return new MinioStrategy();
        }
        return null;
    }
}
